// ws_micro_shop/commons/goodsOpr/goodsOpr.js
var api = require('../../utils/api.js');
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isShow: {
      type: Boolean,
      value: false
    },
    oprtype: {
      type: Number
    },
    oprid: {
      type:Number
    },
    idx: {
      type: Number
    },
    list: {
      type: Array,
      value:[]
    },
    upnum: {
      type: Number
    },
    downnum: {
      type: Number
    },
    useattr: {
      type: String
    },
    iscanedit: {
      type: Boolean,
      value: true
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    //发送操作请求
    operate: function (e) {
      var that = this;
      var _type = e.currentTarget.dataset.opt;
      var id = this.data.oprid;
      var index = this.data.idx;
      var list = this.data.list;
      var upnum = this.data.upnum;
      var downnum = this.data.downnum;
      var oprtype = this.data.oprtype;
      var title = _type== 'up' ? '上架' : (_type == 'down' ? '下架' : '删除');
      wx.showModal({
        content: '是否' + title +'该商品',
        showCancel: true,
        confirmColor: '#4185F4',
        cancelColor: '#333',
        success: res => {
          if (res.confirm) {
            app.ws_micro_shoprequest({
              url: api.adminer.operate,
              data: {
                type: _type,
                goods_id: id
              },
              success: function (res) {
                if (res.code === 0) {
                  var myEventDetail = {};
                  var myEventOption = {};
                  if (_type == 'up') {
                    upnum += 1;
                    downnum -= 1;
                  } else if (_type == 'down') {
                    upnum -= 1;
                    downnum += 1;
                  } else if (_type == 'del') {
                    if (oprtype == 1) {
                      upnum -= 1;
                    } else if (oprtype == 0) {
                      downnum -= 1;
                    }
                  }
                  list.splice(index, 1);
                  myEventDetail = {
                    list: list,
                    myopr: _type,
                    upnum: upnum,
                    downnum: downnum
                  };
                  that.triggerEvent('myopr', myEventDetail, myEventOption)
                } else {
                  wx.showToast({
                    title: res.msg,
                    icon: 'none',
                    duration: 1500
                  })
                }
              }
            });
          }
        },
      });
      
    },
    change: function (e) {
      var that = this;
      var _type = e.currentTarget.dataset.opt;
      var myEventDetail = {};
      var myEventOption = {};
      if (_type == 'price') {
        myEventDetail = {
          show1: true,
          use_attr: that.data.useattr,
          id: that.data.oprid,
        };
        this.triggerEvent('changed', myEventDetail, myEventOption);
      } else if (_type == 'count') {
        myEventDetail = {
          show2: true,
          use_attr: that.data.useattr,
          id: that.data.oprid,
        };
        this.triggerEvent('changed', myEventDetail, myEventOption);
      } else if (_type == 'scan') {
        wx.scanCode({
          success(res) {
        myEventDetail = {
              scanCode: res.result,
        };
            that.triggerEvent('changed', myEventDetail, myEventOption);
          }
        });
      }
    },
  }
})